//
//  DYHomePageViewController.h
//  DaYing
//
//  Created by DaYing Team on 2023.
//  Copyright © 2023 DaYing Team. All rights reserved.
//

#import "DYHomePageViewController.h"

#import "DYMySettingsViewController.h" //设置
#import "DYRemoteControlViewController.h" //遥控器
#import "DYLotteryDecisionViewController.h" //抽签做决定
#import "DYRandomizationViewController.h" //随机分组
#import "DYSubscribeViewController.h"

#import "DYHomeFunctionCell.h"

#import <CoreBluetooth/CoreBluetooth.h>

@interface DYHomePageViewController () <CBCentralManagerDelegate>

@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, assign) NSInteger bluetoothStates;

@end

@implementation DYHomePageViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftItem.hidden = YES;
    [self setRightItemWithIcon:[UIImage imageNamed:@"home_set"] selector:@selector(gotoMySettingsViewController)];

    [self.tableView registerNib:[UINib nibWithNibName:@"DYHomeFunctionCell" bundle:nil] forCellReuseIdentifier:@"DYHomeFunctionCell"];
    
    self.tableView.frame = CGRectMake(0,-self.statusBarHeight, kScreenWidth, kScreenHeight + self.statusBarHeight);
        
    if (!kUserManager.userManager.isSubscribe) {
        
        if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstEnter"] boolValue]) {
            DYSubscribeViewController *vc = [DYSubscribeViewController new];
            vc.modalPresentationStyle = UIModalPresentationFullScreen;
            [self.navigationController presentViewController:vc animated:YES completion:nil];

        } else {

        }
    }
    
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstBlutooth"] boolValue]) {
        [self subPropertiesInit];
    }
}

- (void)subPropertiesInit {// 初始化属性
    // 初始化管理者
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:NO]}];
}

#pragma mark - cellDataSource
    
- (NSArray *)cellDataSource {
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
          
        subarr = [NSMutableArray arrayWithCapacity:1];

        dict =  @{@"class":DYHomeFunctionCell.class,
                  @"height":@([DYHomeFunctionCell getCellFrame:[NSNumber numberWithFloat:kScreenWidth*812/375]]),
                  @"delegate":@YES};
        [subarr addObject:dict];
                
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark DYHomeFunctionCellDelegate 遥控器 抽签做决定 随机分组

- (void)homeFunctionCell:(DYHomeFunctionCell *)cell button:(UIButton *)sender {
    if (!kUserManager.userManager.isSubscribe) {
        [self gotoSubscribeViewController];
        return;
    }
    
    if (sender.tag == 10) {
        if (![[[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstBlutooth"] boolValue]) {
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:@"YES" forKey:@"isFirstBlutooth"];
            [userDefaults synchronize];
            [self subPropertiesInit];
            return;
        }
        //遥控器
        if (_bluetoothStates == 2) {
            // 蓝牙未开启，需要提示用户开启蓝牙
            [kMediaManager showAlertViewAboutNotOpenBluetooth];
        } else if (_bluetoothStates == 1) {
            // 蓝牙未授权，需要提示用户授权蓝牙
            [kMediaManager showAlertViewAboutNotAuthorAccessBluetooth];
        } else {
            // 蓝牙已开启，可以进行蓝牙操作
            [self gotoRemoteControlViewController];
        }
    } else if (sender.tag == 11) {
        //抽签做决定
        [self gotoLotteryDecisionViewController];
    } else if (sender.tag == 12) {
        //随机分组
        [self gotoRandomizationViewController];
    }
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    _bluetoothStates = 0;
    switch(central.state) {
    case CBManagerStateUnknown:
        NSLog(@"central.state = CBManagerStateUnknown");
        break;
        case CBManagerStateResetting:
        {
            //[self showAlertViewTitle:@"Note!" message:@"Your apple device has bluetooth reset, please close the APP and reopen." rescan:NO];
            NSLog(@"central.state = CBManagerStateResetting .........iPhone正在重置蓝牙");
        }
            break;
        case CBManagerStateUnsupported:
            NSLog(@"central.state = CBManagerStateUnsupported");
            break;
        case CBManagerStateUnauthorized:
            NSLog(@"central.state = CBManagerStateUnauthorized");
            _bluetoothStates = 1;
            break;
        case CBManagerStatePoweredOff: {
            NSLog(@"centralManager 未开启蓝牙..");
            _bluetoothStates = 2;
            // 代理监控蓝牙未开启,状态改为正在扫描中
        }
        break;
        case CBManagerStatePoweredOn:
         {
            NSLog(@"centralManager 已开启蓝牙..");
          }
            break;
    }
}

#pragma mark - goto

//设置
- (void)gotoMySettingsViewController {
    DYMySettingsViewController *vc = [DYMySettingsViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoRemoteControlViewController {
    DYRemoteControlViewController *vc = [DYRemoteControlViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//抽签做决定
- (void)gotoLotteryDecisionViewController {
    DYLotteryDecisionViewController *vc = [DYLotteryDecisionViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//随机分组
- (void)gotoRandomizationViewController {
    DYRandomizationViewController *vc = [DYRandomizationViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
